package cn.jietuo.first.class02;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/6/16
 * @description: 一个单链表，把给定的值都删掉
 */
public class Code02_DeleteGivenValue {

    public static Node<Integer> deleteDivenValue(Node head,Integer value){
        // 可能会删头
        while (head != null){
            if (head.value != value){
                break;
            }
            head = head.next;
        }
        // head 来到第一个不需要删的位置
        Node pre = head;
        Node cur = head;
        while (cur != null){
            if (cur.value == value){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    public static void main(String[] args) {
        Node<Integer> node = new Node<Integer>(5);
        node.next = new Node<Integer>(6);
        node.next.next = new Node<Integer>(3);
        node.next.next.next = new Node<Integer>(3);
        Node temp = node;
        while (temp != null){
            System.out.println(temp.value);
            temp = temp.next;
        }
        System.out.println("-------");
        Node<Integer> node1 = deleteDivenValue(node, 6);
        while(node1 !=null){
            System.out.println(node1.value);
            node1 = node1.next;
        }
    }
}
